package com.mumu.system.service.impl;

import com.mumu.dao.BaseDao;
import com.mumu.system.dao.PermissionDao;
import com.mumu.system.model.Permission;
import com.mumu.system.service.PermissionService;
import com.mumu.system.vo.PermissionVO;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class PermissionServiceImpl implements PermissionService {

	@Autowired private BaseDao<Permission> baseDao;
	@Autowired private PermissionDao permissionsDao;
	
	/**
	 * 递归权限
	 */
	public void buildSubs(PermissionVO parent, List<PermissionVO> subs) {
        List<PermissionVO> children = subs.stream().filter(
        								sub -> (sub.getPid().equals(parent.getId()))
        							  ).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(children)) {
            parent.setChildren(children);
            children.forEach(child -> buildSubs(child, subs));
        }
    }
	
	@Override
	public Map<String, Object> getMenuList() {
		List<PermissionVO> permissionsVOList = permissionsDao.getPermissionList();
		/****************************************菜单部分开始*******************************************************/
		//根级菜单
		List<PermissionVO> rootList = permissionsVOList.stream().filter(
			permissioni -> (permissioni.getPid()!=null && permissioni.getPid().equals(0)))
						   .collect(Collectors.toList()
		);
		//一级菜单
		List<PermissionVO> menuList = new ArrayList<PermissionVO>();
		for(PermissionVO rootPermission: rootList) {//遍历根级权限
			for(PermissionVO permission: permissionsVOList) {//遍历所以权限
				if(permission.getPid().equals(rootPermission.getId())) {//筛选一级菜单
					menuList.add(permission);
				}
			}
		}
		//二级菜单
		for(PermissionVO firstMenu: menuList) {//遍历一级菜单
			List<PermissionVO> secondMenuList = new ArrayList<PermissionVO>();
			for(PermissionVO permission: permissionsVOList) {//遍历所以权限
				if(permission.getPid().equals(firstMenu.getId())) {//筛选二级菜单
					secondMenuList.add(permission);
				}
			}
			firstMenu.setChildren(secondMenuList);
		}
		/****************************************菜单部分结束*******************************************************/
		
		Map<String, Object> map = new LinkedHashMap<String, Object>();
		map.put("user", null);
		map.put("permission", permissionsVOList);
		map.put("rootList", rootList);
		map.put("menuList", menuList);
		return map;
	}

	@Override
	public List<PermissionVO> getPermissionList() {
		List<PermissionVO> permissionsVOList = permissionsDao.getPermissionList();
		//根级权限
		List<PermissionVO> rootList = permissionsVOList.stream().filter(
			permissioni -> (permissioni.getPid()!=null && permissioni.getPid().equals(0)))
						   .collect(Collectors.toList()
		);
		//权限子集
		List<PermissionVO> subLists = permissionsVOList.stream().filter(
				permissioni -> (permissioni.getPid()!=null && !permissioni.getPid().equals(0)))
				.collect(Collectors.toList()
		);
		//递归
		rootList.forEach(root -> buildSubs(root,subLists));
		return rootList;
	}
	
	/*
	 * 添加权限
	 * @see com.anchuang.core.system.service.PermissionService#addPermission(com.anchuang.core.system.vo.PermissionVO)
	 */
	@Override
	@Transactional
	public void addPermission(PermissionVO permissionsVO) {
		Permission permission = new Permission();
		BeanUtils.copyProperties(permissionsVO, permission);
		//如果一级菜单id为空，添加一级菜单
		if(permission.getId() == null) {
			baseDao.insert(permission);
		}
		//添加子菜单或功能
		Permission permissionChild = new Permission();
		BeanUtils.copyProperties(permissionsVO.getChildren().get(0), permissionChild);
		permissionChild.setPid(permission.getId());
		baseDao.insert(permissionChild);
	}
	
	/*
	 * 修改权限
	 * @see com.anchuang.core.system.service.PermissionService#editPermission(com.anchuang.core.system.vo.PermissionVO)
	 */
	@Override
	@Transactional
	public void editPermission(PermissionVO permissionVO) {
		Permission permission = new Permission();
		BeanUtils.copyProperties(permissionVO, permission);
		baseDao.updateByPrimaryKeySelective(permission);
	}

	/*
	 * 删除权限
	 * @see com.anchuang.core.system.service.PermissionService#delPermission(java.lang.String)
	 */
	@Override
	@Transactional
	public void delPermission(Integer id) {
		Permission permission = new Permission();
		permission.setId(id);
		baseDao.deleteByPrimaryKey(permission);
	}
	
	
}
